package com.chinacoal.microservice.impl.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chinacoal.microservice.api.CcmsDomainsClient;
import com.chinacoal.microservice.exception.MessageException;
import com.chinacoal.microservice.impl.mapper.ProcessBusinessMapper;
import com.chinacoal.microservice.impl.service.ProcessBusinessService;
import com.chinacoal.microservice.impl.util.ProcessFun;
import com.chinacoal.microservice.model.rbac.CcmsDomains;
import com.chinacoal.microservice.model.rbac.bo.DomainBO;
import com.chinacoal.microservice.model.wofkflow.QueryProcessBusinessParam;
import com.chinacoal.microservice.model.wofkflow.entity.ProcessBusinessEntity;
import com.chinacoal.microservice.util.PrimaryKey;
import com.chinacoal.microservice.util.result.Result;

/**  
 * @Package: com.chinacoal.microservice.impl.service.impl.ProcessBusinessServiceImpl
 * @ClassName: ProcessBusinessServiceImpl
 * @ClassDesc: 业务流程授权ServiceImpl
 * @Createby: sunh
 * @CreatedDate: 2019-12-03 15:15:56
 */  
@Service
public class ProcessBusinessServiceImpl extends ServiceImpl<ProcessBusinessMapper, ProcessBusinessEntity> implements ProcessBusinessService {

	@Resource
	private ProcessBusinessMapper processBusinessMapper;
	@Resource
	private CcmsDomainsClient domainsClient;
	
	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public Page<ProcessBusinessEntity> findList(Integer page, Integer pageSize, QueryProcessBusinessParam queryParam) throws Exception {
		
		Page<ProcessBusinessEntity> pages= ProcessFun.getPage(page, pageSize);
		
		// 获取当前用户的系统权限
		DomainBO domainBo = new DomainBO();
		domainBo.setUserId(queryParam.getUserId());
		Result<List<CcmsDomains>> domainResult = domainsClient.getDomainAuth(domainBo);
		
		if(domainResult.getCode() == 10000) {
			List<CcmsDomains> domainAuthList = domainResult.getData();
			
			if(domainAuthList == null || domainAuthList.size() < 0) {
				return pages.setRecords(new ArrayList<ProcessBusinessEntity>());
			}
			
			// 获取当前用户的机构权限
			
			
			// 获取权限下的配置关系列表
			queryParam.setDomainAuthList(domainAuthList);
			List<ProcessBusinessEntity> processBusinessList = processBusinessMapper.findList(pages, queryParam);
			
			return pages.setRecords(processBusinessList);
			
		}else {
			
			throw new MessageException(domainResult.getMsg());
		}
	}

	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public ProcessBusinessEntity getProcessByBusinessCode(Integer orgId, String sysCode, String businessCode) throws Exception {

		Result<CcmsDomains> domainResult  = domainsClient.findDomainByIamClientId(sysCode);
		
		if(domainResult.getCode() != 10000) {
			throw new MessageException("未获取到系统，系统编码：" + sysCode);
		}
		
		String domainId = domainResult.getData().getDomainId();
		
		return processBusinessMapper.getProcessByBusinessCode(orgId, domainId, businessCode);
	}
	
	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public String insertProcessBusiness(ProcessBusinessEntity processBusinessEntity) throws Exception {
		
		// 新增数据校验
		String businessCode = processBusinessEntity.getBusinessCode();
		Integer orgId = processBusinessEntity.getOrgId();
		String domainId = processBusinessEntity.getDomainId();
		Integer num = processBusinessMapper.getProcessBusinessCount(orgId, domainId, businessCode);
		if(num > 0) throw new Exception("当前业务类型已经绑定了流程，无法再次进行流程绑定");
		
		// 数据补全
		processBusinessEntity.setMapId(PrimaryKey.newKey());
		
		// 执行新增
		processBusinessMapper.insert(processBusinessEntity);
		
		return processBusinessEntity.getMapId();
	}

	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public String updateProcessBusiness(ProcessBusinessEntity processBusinessEntity) throws Exception{
		
		processBusinessMapper.updateById(processBusinessEntity);
		
		return processBusinessEntity.getMapId();
	}

}
